﻿Imports System.IO
Imports System.Text.Json

Public Class 界面控制_编码队列

    Public Shared Sub 开始任务()
        If Form1.ListView1.SelectedItems.Count = 0 Then Exit Sub
        If 编码任务.队列.Where(Function(item) item.状态 = 编码任务.编码状态.正在处理).Count() >= 10 Then
            Dim 选项字典 As New Dictionary(Of String, Action)
            选项字典("了解") = Nothing
            选项字典("确认开始更多的任务") = AddressOf 真的开始任务
            软件内对话框.显示对话框(Form1, "同时进行的任务过多", "正在进行的任务已经 ≥ 10 个，确定要继续开始新任务吗？ffmpeg 使用的资源过多可能导致系统运行缓慢甚至各种崩溃!", 选项字典, 软件内对话框.主题类型.错误)
        Else
            真的开始任务()
        End If
    End Sub

    Private Shared Sub 真的开始任务()
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            Dim i = item.Index
            Select Case 编码任务.队列(i).状态
                Case 编码任务.编码状态.未处理
                    Task.Run(AddressOf 编码任务.队列(i).开始)
            End Select
        Next
    End Sub

    Public Shared Sub 暂停任务()
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            Dim i = item.Index
            Select Case 编码任务.队列(i).状态
                Case 编码任务.编码状态.正在处理
                    编码任务.队列(i).暂停()
            End Select
        Next
    End Sub

    Public Shared Sub 恢复任务()
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            Dim i = item.Index
            Select Case 编码任务.队列(i).状态
                Case 编码任务.编码状态.已暂停
                    编码任务.队列(i).恢复()
            End Select
        Next
    End Sub

    Public Shared Sub 停止任务()
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            Dim i = item.Index
            Select Case 编码任务.队列(i).状态
                Case 编码任务.编码状态.正在处理, 编码任务.编码状态.已暂停, 编码任务.编码状态.错误
                    编码任务.队列(i).停止()
            End Select
        Next
    End Sub

    Public Shared Sub 移除任务()
        Dim indices As New List(Of Integer)
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            Dim i = item.Index
            Select Case 编码任务.队列(i).状态
                Case 编码任务.编码状态.已完成, 编码任务.编码状态.未处理, 编码任务.编码状态.错误, 编码任务.编码状态.已停止
                    indices.Add(i)
            End Select
        Next
        indices.Sort()
        indices.Reverse()
        For Each i In indices
            编码任务.队列(i).清除占用()
            编码任务.队列.RemoveAt(i)
        Next
    End Sub

    Public Shared Sub 重置任务()
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            Dim i = item.Index
            Select Case 编码任务.队列(i).状态
                Case 编码任务.编码状态.已完成, 编码任务.编码状态.错误, 编码任务.编码状态.已停止
                    编码任务.队列(i).状态 = 编码任务.编码状态.未处理
                    编码任务.队列(i).任务耗时计时器.Reset()
                    编码任务.队列(i).状态刷新统一逻辑()
            End Select
        Next
    End Sub

    Public Shared Sub 全选任务()
        For Each item As ListViewItem In Form1.ListView1.Items
            item.Selected = True
        Next
    End Sub

    Public Shared Sub 反选任务()
        For Each item As ListViewItem In Form1.ListView1.Items
            item.Selected = Not item.Selected
        Next
    End Sub

    Public Shared Sub 选中所有出错的任务()
        For Each item As ListViewItem In Form1.ListView1.Items
            Select Case 编码任务.队列(item.Index).状态
                Case 编码任务.编码状态.错误 : item.Selected = True
                Case Else : item.Selected = False
            End Select
        Next
        If Form1.ListView1.SelectedItems.Count > 0 Then Form1.ListView1.EnsureVisible(Form1.ListView1.SelectedItems(0).Index)
    End Sub

    Public Shared Sub 定位输出()
        If Form1.ListView1.SelectedItems.Count <> 1 Then Exit Sub
        Dim 输出文件 = 编码任务.队列(Form1.ListView1.SelectedItems(0).Index).输出文件
        Select Case 编码任务.队列(Form1.ListView1.SelectedItems(0).Index).状态
            Case 编码任务.编码状态.未处理
                If Not FileIO.FileSystem.FileExists(编码任务.队列(Form1.ListView1.SelectedItems(0).Index).输入文件) Then Exit Sub
                Dim 选项字典 As New Dictionary(Of String, Action)
                选项字典("不是") = Nothing
                选项字典("当然") = Sub() Process.Start("explorer", "/select,""" & 编码任务.队列(Form1.ListView1.SelectedItems(0).Index).输入文件 & """")
                软件内对话框.显示对话框(Form1, "你指的是定位输入文件吗？", "因为此任务还未开始。", 选项字典, 软件内对话框.主题类型.常规)
            Case Else
                If Not FileIO.FileSystem.FileExists(编码任务.队列(Form1.ListView1.SelectedItems(0).Index).输出文件) Then Exit Sub
                Process.Start("explorer", "/select,""" & 编码任务.队列(Form1.ListView1.SelectedItems(0).Index).输出文件 & """")
        End Select
    End Sub

    Public Shared Sub 复制多个任务的命令行()
        If Form1.ListView1.SelectedItems.Count = 0 Then Exit Sub
        Dim 每个任务的命令行 As New List(Of String)
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            If 编码任务.队列(item.Index).命令行 <> "" Then
                每个任务的命令行.Add("ffmpeg " & 编码任务.队列(item.Index).命令行)
            Else
                If 编码任务.队列(item.Index).预设数据 IsNot Nothing Then
                    每个任务的命令行.Add("ffmpeg " & 预设管理.将预设数据转换为命令行(编码任务.队列(item.Index).预设数据, 编码任务.队列(item.Index).输入文件, 编码任务.队列(item.Index).输出文件))
                Else
                    每个任务的命令行.Add("没有可用的命令行")
                End If
            End If
        Next
        Clipboard.SetText(String.Join(vbCrLf, 每个任务的命令行))
    End Sub

    Public Shared Sub 将参数面板数据覆盖到任务()
        If Form1.ListView1.SelectedItems.Count = 0 Then Exit Sub
        Dim a As New 预设数据类型
        预设管理.储存预设(a, Form1.常规流程参数页面)
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            编码任务.队列(item.Index).预设数据 = a
        Next
    End Sub

    Public Shared Sub 将任务参数覆盖到参数面板()
        If Form1.ListView1.SelectedItems.Count <> 1 Then Exit Sub
        If 编码任务.队列(Form1.ListView1.SelectedItems(0).Index).预设数据 Is Nothing Then
            MsgBox("此任务不包含 3FUI 的预设数据，一般是由其他程序添加的，这样不能使用这个功能。", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        If MsgBox("确定将此任务的配置数据用于覆盖参数面板中的设置？此操作不可逆！", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            预设管理.显示预设(编码任务.队列(Form1.ListView1.SelectedItems(0).Index).预设数据, Form1.常规流程参数页面)
        End If
    End Sub

    Public Shared Sub 将任务放到添加文件选项卡()
        If Form1.ListView1.SelectedItems.Count = 0 Then Exit Sub
        Dim 已有的文件 As New List(Of String)
        For Each item As ListViewItem In Form1.ListView2.Items
            已有的文件.Add(item.Text)
        Next
        For Each item As ListViewItem In Form1.ListView1.SelectedItems
            If Not 已有的文件.Contains(编码任务.队列(item.Index).输入文件) Then
                Form1.ListView2.Items.Add(编码任务.队列(item.Index).输入文件)
            End If
        Next
    End Sub

    Public Shared Sub 导出任务参数数据到预设文件()
        If Form1.ListView1.SelectedItems.Count <> 1 Then Exit Sub
        If 编码任务.队列(Form1.ListView1.SelectedItems(0).Index).预设数据 Is Nothing Then
            MsgBox("此任务不包含 3FUI 的预设数据，不能使用这个功能。", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        Dim d As New SaveFileDialog With {.Filter = "Json|*.json"}
        d.ShowDialog(Form1)
        If d.FileName = "" Then Exit Sub
        File.WriteAllText(d.FileName, JsonSerializer.Serialize(编码任务.队列(Form1.ListView1.SelectedItems(0).Index).预设数据, JsonSO))
    End Sub

    Public Shared Sub 上移任务()
        If Form1.ListView1.SelectedItems.Count = 0 Then Exit Sub
        SyncLock 编码任务.队列
            Dim indices As New List(Of Integer)
            For Each item As ListViewItem In Form1.ListView1.SelectedItems
                indices.Add(item.Index)
            Next
            indices.Sort()
            For Each i In indices
                If i > 0 Then
                    Dim temp = 编码任务.队列(i - 1)
                    编码任务.队列(i - 1) = 编码任务.队列(i)
                    编码任务.队列(i) = temp
                    Dim item1 = Form1.ListView1.Items(i - 1)
                    Dim item2 = Form1.ListView1.Items(i)
                    Dim wasSelected1 = item1.Selected
                    Dim wasSelected2 = item2.Selected
                    Form1.ListView1.Items.RemoveAt(i)
                    Form1.ListView1.Items.RemoveAt(i - 1)
                    Form1.ListView1.Items.Insert(i - 1, item2)
                    Form1.ListView1.Items.Insert(i, item1)
                    Form1.ListView1.Items(i - 1).Selected = wasSelected2
                    Form1.ListView1.Items(i).Selected = wasSelected1
                    编码任务.队列(i - 1).列表视图项 = Form1.ListView1.Items(i - 1)
                    编码任务.队列(i).列表视图项 = Form1.ListView1.Items(i)
                End If
            Next
        End SyncLock
    End Sub

    Public Shared Sub 下移任务()
        If Form1.ListView1.SelectedItems.Count = 0 Then Exit Sub
        SyncLock 编码任务.队列
            Dim indices As New List(Of Integer)
            For Each item As ListViewItem In Form1.ListView1.SelectedItems
                indices.Add(item.Index)
            Next
            indices.Sort()
            indices.Reverse()
            For Each i In indices
                If i < 编码任务.队列.Count - 1 Then
                    Dim temp = 编码任务.队列(i + 1)
                    编码任务.队列(i + 1) = 编码任务.队列(i)
                    编码任务.队列(i) = temp
                    Dim item1 = Form1.ListView1.Items(i)
                    Dim item2 = Form1.ListView1.Items(i + 1)
                    Dim wasSelected1 = item1.Selected
                    Dim wasSelected2 = item2.Selected
                    Form1.ListView1.Items.RemoveAt(i + 1)
                    Form1.ListView1.Items.RemoveAt(i)
                    Form1.ListView1.Items.Insert(i, item2)
                    Form1.ListView1.Items.Insert(i + 1, item1)
                    Form1.ListView1.Items(i).Selected = wasSelected2
                    Form1.ListView1.Items(i + 1).Selected = wasSelected1
                    编码任务.队列(i).列表视图项 = Form1.ListView1.Items(i)
                    编码任务.队列(i + 1).列表视图项 = Form1.ListView1.Items(i + 1)
                End If
            Next
        End SyncLock
    End Sub

    Public Shared Sub 置顶已有的错误任务()
        SyncLock 编码任务.队列
            Dim indices As New List(Of Integer)
            For Each item As ListViewItem In Form1.ListView1.Items
                Select Case 编码任务.队列(item.Index).状态
                    Case 编码任务.编码状态.错误
                        indices.Add(item.Index)
                End Select
            Next
            If indices.Count > 0 Then
                indices.Sort()
                For Each i In indices
                    If i > 0 Then
                        Dim temp = 编码任务.队列(i)
                        编码任务.队列.RemoveAt(i)
                        编码任务.队列.Insert(0, temp)
                        Dim item1 = Form1.ListView1.Items(i)
                        Form1.ListView1.Items.RemoveAt(i)
                        Form1.ListView1.Items.Insert(0, item1)
                        For a As Integer = 0 To i
                            编码任务.队列(a).列表视图项 = Form1.ListView1.Items(a)
                        Next
                    End If
                Next
            End If
        End SyncLock
    End Sub
End Class
